package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLikedDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.dto.SubjectLikedDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.utils.LoginUtil;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectLikedBO;
import com.jingdianjichi.subject.domain.service.SubjectLikedDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/subjectLiked")
@Slf4j
public class SubjectLikedController {
    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    /**
     * 新增题目点赞表,这个add做的既是点赞和取消点赞都在这里,就是依赖前端传递的status这个字段,我们就能区分出来了
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "点赞状态不能为空");

            //获得用户id
            String loginId = LoginUtil.getLoginId();
            subjectLikedDTO.setLikeUserId(loginId);     //放入Dto
            Preconditions.checkNotNull(subjectLikedDTO.getLikeUserId(), "点赞人不能为空");

            SubjectLikedBO SubjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);

            //这里是使用redis+xxl实现的
            //subjectLikedDomainService.add(SubjectLikedBO);

            //这里我们升级为使用mq实现
            subjectLikedDomainService.add_plus(SubjectLikedBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLikedController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增题目点赞表失败");
        }

    }

    /**
     * 查询我的点赞列表
     */
    @PostMapping("/getSubjectLikedPage")
    public Result<PageResult<SubjectLikedDTO>> getSubjectLikedPage(@RequestBody SubjectLikedDTO subjectLikedDTO){
        //左逻辑判断
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.getSubjectLikedPage.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }

            //转换对象
            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);

            PageResult<SubjectLikedBO> boPageResult=subjectLikedDomainService.getSubjectLikePage(subjectLikedBO);

            PageResult<SubjectLikedDTO> dtoPageResult= SubjectLikedDTOConverter.INSTANCE.convertBoPageToDTOPageList(boPageResult);

            return Result.ok(dtoPageResult);
        }catch (Exception e){
            log.error("SubjectCategoryController.getSubjectLikedPage.error:{}",e.getMessage(),e);

            return Result.fail(e.getMessage());
        }
    }

}
